package cn.learn.controller.baseinfo;

import cn.learn.base.BaseController;
import cn.learn.domain.cargo.Factory;
import cn.learn.domain.cargo.FactoryExample;
import cn.learn.service.cargo.FactoryService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/baseinfo/factory")
public class FactoryController extends BaseController {

    @Reference
    private FactoryService factoryService;

    /**
     * 查看厂家信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list", name = "工厂列表")
    public String list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.setOrderByClause("create_time desc");
        PageInfo resultData = factoryService.findAll(factoryExample, pageNum, pageSize);
        request.setAttribute("pageList", resultData);
        return "/baseinfo/factory/factory-list";
    }

    /**
     * 添加厂商信息
     * @param factory
     * @return
     */
    @RequestMapping(value = "/addOne", name = "添加新的厂家信息")
    public String addOne(Factory factory) {
        if (request.getMethod().equals("POST")) {
            factory.setCreateBy(user.getUserName());
            factory.setUpdateBy(user.getUserName());
            factory.setCreateDept(user.getDeptName());
            factoryService.save(factory);
            return "redirect:/baseinfo/factory/list.do";
        }
        return "/baseinfo/factory/factory-add";
    }

    /**
     * 修改厂家信息
     * @param factory
     * @return
     */
    @RequestMapping(value = "/updateById", name = "修改厂家信息")
    public String updateById(Factory factory) {
        if (request.getMethod().equals("POST")) {
            factory.setUpdateBy(user.getUserName());
            factoryService.update(factory);
            return "redirect:/baseinfo/factory/list.do";
        }
        Factory resultFactory = factoryService.findById(factory.getId());
        request.setAttribute("data", resultFactory);
        return "/baseinfo/factory/factory-update";
    }

    @RequestMapping(value = "delete", name = "删除厂家信息")
    public String delete(String id) {
        factoryService.delete(id);
        return "redirect:/baseinfo/factory/list.do";
    }

}
